//
//  ViewController.m
//  Homework_26
//
//  Created by 徐昊 on 16/8/18.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "Masonry.h"
#define WIDTH self.view.frame.size.width
@interface ViewController () {
    NSDateFormatter *_dateFormatter;
    NSString *_totalTime;
}

@property (nonatomic, strong) AVAudioPlayer *player;

@property (nonatomic, strong) UIImageView *albumPicture;

@property (nonatomic, strong) UISlider *slider;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) UILabel *currentTime;

@property (nonatomic, strong) UILabel *songTime;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self config];
}

- (void)config {
    UIImageView *imageView = [UIImageView new];
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    imageView.image = [UIImage imageNamed:@"{1B13CD84-A66C-7E19-8E05-073C4C1787B6}"];
#pragma mark - 毛玻璃
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:effect];
    visualView.frame = [[UIScreen mainScreen] bounds];
    [imageView addSubview:visualView];
    visualView.alpha = 0.9;
#pragma mark - 初始化播放器
    NSString *path = [[NSBundle mainBundle] pathForResource:@"森永真由美,Another Infinity - 响音 -kotone-" ofType:@"mp3"];
    // 播放网络歌曲(AVAudioPlayer不支持网上播放, 需要把歌曲下载到本地然后进行播放)
//    NSURL *url = [NSURL fileURLWithPath:path];
//    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://150.138.167.87/flv2.bn.netease.com/videolib3/1505/24/HYUCE6348/SD/HYUCE6348-mobile.mp4?wsiphost=local"]];
//    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp4", docDirPath , @"temp"];
//    [data writeToFile:filePath atomically:YES];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    NSLog(@"%g", self.player.duration);
    self.player.numberOfLoops = 10;
    // 歌曲名Label
    UILabel *songName = [UILabel new];
    [self.view addSubview:songName];
    [songName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).multipliedBy(1);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(25);
        make.width.mas_equalTo(100);
    }];
    songName.text = @"战争世界";
    songName.textColor = [UIColor whiteColor];
    songName.textAlignment = NSTextAlignmentCenter;
    songName.font = [UIFont systemFontOfSize:20];
    // 歌手Label
    UILabel *singer = [UILabel new];
    [self.view addSubview:singer];
    [singer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).multipliedBy(1);
        make.top.mas_equalTo(songName.mas_bottom).mas_equalTo(5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];
    singer.text = @"- 姚贝娜 -";
    singer.textColor = [UIColor whiteColor];
    singer.textAlignment = NSTextAlignmentCenter;
    singer.font = [UIFont systemFontOfSize:16];
    // 左上角下拉菜单
    UIButton *pullLeft = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:pullLeft];
    [pullLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(70);
    }];
    [pullLeft setImage:[UIImage imageNamed:@"player_btn_close_normal"] forState:UIControlStateNormal];
    pullLeft.tintColor = [UIColor whiteColor];
    // 右上角菜单选项
    UIButton *menu = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:menu];
//    menu.frame = CGRectMake(WIDTH - 75, 20, 60, 60);
    [menu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
        
    }];
    [menu setImage:[UIImage imageNamed:@"player_btn_more_normal"] forState:UIControlStateNormal];
    menu.tintColor = [UIColor whiteColor];
    // 独家
    UIButton *kind = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:kind];
//    kind.frame = CGRectMake(150, 100, 50, 25);
    [kind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).mas_equalTo(-30);
        make.top.mas_equalTo(singer.mas_bottom).mas_equalTo(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(25);
    }];
    [kind setImage:[UIImage imageNamed:@"player_btn_copyright_normal"] forState:UIControlStateNormal];
    kind.tintColor = [UIColor greenColor];
    // 流畅
    UIButton *tone = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:tone];
//    tone.frame = CGRectMake(90, 100, 50, 25);
    [tone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kind.mas_width);
        make.height.mas_equalTo(kind.mas_height);
        make.right.mas_equalTo(kind.mas_left).mas_equalTo(-10);
        make.centerY.mas_equalTo(kind.mas_centerY);
    }];
    [tone setImage:[UIImage imageNamed:@"player_btn_lc_sel_normal"] forState:UIControlStateNormal];
    tone.tintColor = [UIColor greenColor];
    // MV
    UIButton *MV = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:MV];
//    MV.frame = CGRectMake(210, 100, 50, 25);
    [MV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kind.mas_width);
        make.height.mas_equalTo(kind.mas_height);
        make.left.mas_equalTo(kind.mas_right).mas_equalTo(10);
        make.centerY.mas_equalTo(kind.mas_centerY);
    }];
    [MV setImage:[UIImage imageNamed:@"player_btn_mv_normal"] forState:UIControlStateNormal];
    MV.tintColor = [UIColor greenColor];
    // dts
    UIButton *dts = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:dts];
    dts.frame = CGRectMake(270, 100, 50, 25);
    [dts mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kind.mas_width);
        make.height.mas_equalTo(kind.mas_height);
        make.left.mas_equalTo(MV.mas_right).mas_equalTo(10);
        make.centerY.mas_equalTo(kind.mas_centerY);
        
    }];
    [dts setImage:[UIImage imageNamed:@"player_btn_dts_off"] forState:UIControlStateNormal];
    dts.tintColor = [UIColor grayColor];
    // 专辑图片
    _albumPicture = [UIImageView new];
    [self.view addSubview:_albumPicture];
    [_albumPicture mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY).mas_equalTo(-50);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.7);
        make.height.mas_equalTo(_albumPicture.mas_width);
    }];
    [_albumPicture setImage:[UIImage imageNamed:@"06532116-54AE-40F7-A0D8-6D25D5CD8C47"]];
    _albumPicture.layer.cornerRadius = self.view.bounds.size.width * 3.5 / 10.0;
    _albumPicture.layer.masksToBounds = YES;
    [self initAnimatiom];
    [self pauseLayer:_albumPicture.layer];
    // 歌词
    UILabel *lyric = [UILabel new];
    [self.view addSubview:lyric];
    [lyric mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(_albumPicture.mas_bottom).mas_equalTo(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(160);
    }];
    lyric.text = @"战争世界 - 姚贝娜";
    lyric.textAlignment = NSTextAlignmentCenter;
    lyric.textColor = [UIColor whiteColor];
    // pageControl
    UIPageControl *pageControl = [UIPageControl new];
    [self.view addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(lyric.mas_bottom).mas_equalTo(0);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(60);
    }];
    pageControl.numberOfPages = 3;
    pageControl.currentPage = 1;
#pragma mark - Slider
    _slider = [UISlider new];
    [self.view addSubview:_slider];
    [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(pageControl.mas_bottom).mas_equalTo(0);
        make.width.mas_equalTo(280);
        make.height.mas_equalTo(30);
        
    }];
    [_slider setMinimumTrackImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [_slider setMaximumTrackImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
    [_slider setThumbImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
    [_slider addTarget:self action:@selector(changeTime) forControlEvents:UIControlEventValueChanged];
    _slider.maximumValue = self.player.duration;
    _totalTime = [self convertTime:self.player.duration];
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];
    
#pragma mark - 进度时间
    // 当前播放进度时间
    _currentTime = [UILabel new];
    [self.view addSubview:_currentTime];
    [_currentTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_slider);
        make.right.mas_equalTo(_slider.mas_left);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
        
    }];
    _currentTime.text = @"0:00";
    _currentTime.textAlignment = NSTextAlignmentCenter;
    _currentTime.textColor = [UIColor whiteColor];
#pragma mark - 曲目总时长
    // 曲目的总时长
    _songTime = [UILabel new];
    [self.view addSubview:_songTime];
    [_songTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_slider);
        make.left.mas_equalTo(_slider.mas_right);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
        
    }];
    _songTime.text = _totalTime;
    _songTime.textColor = [UIColor whiteColor];
    _songTime.textAlignment = NSTextAlignmentCenter;
    // 播放键
    UIButton *play = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:play];
//    play.frame = CGRectMake(WIDTH / 2 - 40, 560, 80, 80);
    [play mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(_slider.mas_bottom).mas_equalTo(5);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    [play setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    play.tintColor = [UIColor greenColor];
    [play addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    
    // 上一曲
    UIButton *previous = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:previous];
//    previous.frame = CGRectMake(WIDTH / 2 - 120, 570, 60, 60);
    [previous mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(play.mas_centerX).mas_equalTo(-90);
        make.centerY.mas_equalTo(play.mas_centerY);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
        
    }];
    [previous setImage:[UIImage imageNamed:@"player_btn_pre_normal"] forState:UIControlStateNormal];
    previous.tintColor = [UIColor greenColor];
    // 下一曲
    UIButton *next = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:next];
//    next.frame = CGRectMake(WIDTH / 2 + 60, 570, 60, 60);
    [next mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(play.mas_centerX).mas_equalTo(90);
        make.centerY.mas_equalTo(play.mas_centerY);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
        
    }];
    [next setImage:[UIImage imageNamed:@"player_btn_next_normal"] forState:UIControlStateNormal];
    next.tintColor = [UIColor greenColor];
    // 下载
    UIButton *download = [UIButton buttonWithType:UIButtonTypeSystem];
//    download.frame = CGRectMake(WIDTH / 2 - 25, 650, 50, 50);
    [self.view addSubview:download];
    [download mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(play.mas_bottom);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
        
    }];
    [download setImage:[UIImage imageNamed:@"player_btn_download_pay_normal"] forState:UIControlStateNormal];
    download.tintColor = [UIColor grayColor];
    // 循环
    UIButton *circulation = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:circulation];
//    circulation.frame = CGRectMake(WIDTH / 2 - 85, 650, 50, 50);
    [circulation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(download.mas_left).mas_equalTo(-12.5);
        make.centerY.mas_equalTo(download.mas_centerY);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
        
    }];
    [circulation setImage:[UIImage imageNamed:@"player_btn_repeat_normal"] forState:UIControlStateNormal];
    circulation.tintColor = [UIColor grayColor];
    // 喜欢
    UIButton *love = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:love];
//    love.frame = CGRectMake(WIDTH / 2 - 145, 650, 50, 50);
    [love mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(circulation.mas_left).mas_equalTo(-12.5);
        make.centerY.mas_equalTo(download.mas_centerY);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
        
    }];
    [love setImage:[UIImage imageNamed:@"player_btn_favorited_normal"] forState:UIControlStateNormal];
    love.tintColor = [UIColor redColor];
    // 分享
    UIButton *share = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:share];
//    share.frame = CGRectMake(WIDTH / 2 + 35, 650, 50, 50);
    [share mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(download.mas_right).mas_equalTo(12.5);
        make.centerY.mas_equalTo(download.mas_centerY);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
        
    }];
    [share setImage:[UIImage imageNamed:@"player_btn_share_highlight"] forState:UIControlStateNormal];
    share.tintColor = [UIColor grayColor];
    // 歌曲列表
    UIButton *list = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:list];
//    list.frame = CGRectMake(WIDTH / 2 + 95, 650, 50, 50);
    [list mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(share.mas_right).mas_equalTo(12.5);
        make.centerY.mas_equalTo(download.mas_centerY);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
        
    }];
    [list setImage:[UIImage imageNamed:@"playing_recommend_floder"] forState:UIControlStateNormal];
    list.tintColor = [UIColor grayColor];
}
- (void)initAnimatiom {
    //创建一个绕z轴选择的动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转一周
    animation.toValue = @(2 * M_PI);
    //循环次数，尽量大
    animation.repeatCount = MAXFLOAT;
    //执行一次动画需要的时间
    animation.duration = 50.0f;
    [self.albumPicture.layer addAnimation:animation forKey:@"rotationAnimation"];
}
#pragma mark - 方法实现
- (void)changeTime {
    _player.currentTime = _slider.value;
}
- (void)changeValue {
    _slider.value = _player.currentTime;
    _currentTime.text = [self convertTime:_player.currentTime];
}
- (NSString *)convertTime:(CGFloat)second{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    if (second/3600 >= 1) {
        [[self dateFormatter] setDateFormat:@"HH:mm:ss"];
    } else {
        [[self dateFormatter] setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [[self dateFormatter] stringFromDate:d];
    return showtimeNew;
}
- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
    }
    return _dateFormatter;
}
- (void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}
- (void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    
    
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}
/** 点击播放键和暂停键进行切换 */
- (void)play:(UIButton *)play {
    [self.player play];
    [play removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [play addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [play setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [self resumeLayer:_albumPicture.layer];
}
- (void)pause:(UIButton *)pause {
    [self.player pause];
    [pause removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [pause addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [pause setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [self pauseLayer:_albumPicture.layer];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
